package com.gitee.huanminabc.fastboot.load.classload;

import com.gitee.huanminabc.jcommon.reflect.ClassUtil;
import com.gitee.huanminabc.jcommon.reflect.GenericUtil;
import com.gitee.huanminabc.fastboot.context.AppContext;
import com.gitee.huanminabc.fastboot.event.AppEvent;
import com.gitee.huanminabc.fastboot.event.AppListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 简要描述
 *
 * @Author: huanmin
 * @Date: 2025/4/13 00:44
 * @Version: 1.0
 * @Description: 文件作用详细描述....
 */
public class AppListenerClassLoad  implements ClassLoadIntercept {
    @Override
    public void peek(Class<?> clazz) throws Exception {
        if (AppListener.class.isAssignableFrom(clazz)) {
            Object newInstance = ClassUtil.newInstance(clazz);
            Class<AppEvent> aClass = (Class<AppEvent>) GenericUtil.getClassInterfaceGeneric(clazz, AppListener.class, 0);
            List<AppListener<AppEvent>> appListeners = AppContext.appListenerMap.computeIfAbsent(aClass, (k) -> new ArrayList<>());
            appListeners.add((AppListener<AppEvent>) newInstance);
        }
    }
}
